package com.vhall.component.service.room.impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.vhall.component.entity.guosen.entity.GuosenOrgEntity;
import com.vhall.component.entity.rbac.enums.RoleTypeEnum;
import com.vhall.component.entity.room.dto.SelectOAFlowDTO;
import com.vhall.component.entity.room.vo.*;
import com.vhall.component.plugin.common.constant.RedisKey;
import com.vhall.component.plugin.common.support.Token;
import com.vhall.component.plugin.common.support.TokenThreadLocal;
import com.vhall.component.plugin.core.service.GuoXinService;
import com.vhall.component.service.account.GuosenStaffService;
import com.vhall.component.service.guosen.GuosenOrgService;
import com.vhall.component.service.room.InvestRefService;
import com.vhall.component.service.room.RoomThirdService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * RoomThirdServiceImpl-
 *
 * @author xing.wang
 * @date 2023/8/8
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class RoomThirdServiceImpl implements RoomThirdService {
    private final InvestRefService investRefService;

    private final GuoXinService guoXinService;

    private final GuosenStaffService guosenStaffService;
    private final GuosenOrgService guosenOrgService;
    private final RedisTemplate<String,String> redisTemplate;

    @Override
    public List<InvestRefResponseVO> getGuoXinInvestRefList(InvestRefRequestVO vo) {
        return investRefService.getGuoXinInvestRefList(vo);
    }

    @Override
    public List<OAFlowResponseVO> getGuoXinOAFlowList(OAFlowRequestVO vo) {
        SelectOAFlowDTO dto = new SelectOAFlowDTO();
        Token token = TokenThreadLocal.getToken();
        dto.setNotesid(token.getThirdPartyUserId());
        dto.setTitle(vo.getTitle());
        dto.setFdId(vo.getFdId());
        List<OAFlowResponseVO> result = new ArrayList<>();
        JSONObject oaFlowList = Optional.ofNullable(guoXinService.getOAFlowList(dto)).orElse(new JSONObject());
        log.info(oaFlowList.toString());
        JSONArray oaFlowListJSONArray = oaFlowList.getJSONArray("list");
        if (Objects.nonNull(oaFlowListJSONArray)) {
            int size = oaFlowListJSONArray.size();
            for (int i = 0; i < size; i++) {
                JSONObject jsonObject = oaFlowListJSONArray.getJSONObject(i);
                OAFlowResponseVO responseVO = buildOAFlowInfo(jsonObject);
                result.add(responseVO);
            }
        }
        return result;
    }

    @Override
    public List<GuoSenStaffListResponseVO> getGuoXinStaffList(GuoSenStaffListRequestVO vo) {
        return guosenStaffService.getGuoXinStaffList(vo);
    }

    @Override
    public List<GetGuoXinOrgListResponseVO> getGuoXinOrgList(GetGuoXinOrgListRequestVO vo) {
        List<GetGuoXinOrgListResponseVO> orgListResponseVOS ;
        String key = RedisKey.GUO_SEN_ORG_ALL_LIST;
        if (redisTemplate.hasKey(key)) {
            String listJsonStr = redisTemplate.opsForValue().get(key);
            orgListResponseVOS = JSONObject.parseArray(listJsonStr,GetGuoXinOrgListResponseVO.class);
        }else{
            LambdaQueryWrapper<GuosenOrgEntity> queryWrapper = Wrappers.lambdaQuery();
            List<GuosenOrgEntity> list = guosenOrgService.list(queryWrapper);
            orgListResponseVOS = list.stream().map(data -> {
                GetGuoXinOrgListResponseVO temp = new GetGuoXinOrgListResponseVO();
                BeanUtil.copyProperties(data, temp);
                return temp;
            }).collect(Collectors.toList());
            redisTemplate.opsForValue().set(key,JSONObject.toJSONString(orgListResponseVOS),1, TimeUnit.HOURS);
        }
        Token token = TokenThreadLocal.getToken();
        Integer roleType = token.getRoleType();
        if (RoleTypeEnum.DEPT_LEVEL.getValue().equals(roleType)) {
            String orgCode = token.getOrgCode();
            orgListResponseVOS = orgListResponseVOS.stream().filter(data->Objects.equals(data.getCode(),orgCode)).collect(Collectors.toList());
        }
        return orgListResponseVOS;
    }

    @NotNull
    private OAFlowResponseVO buildOAFlowInfo(JSONObject jsonObject) {
        OAFlowResponseVO responseVO = new OAFlowResponseVO();
        //流程id
        String fdId = jsonObject.getString("fd_id");
        //流程 URL
        String mhtUrl = jsonObject.getString("mht_url");
        //拟稿人名称
        String fdName = jsonObject.getString("fd_name");
        //拟稿人工号
        String fdNo = jsonObject.getString("fd_no");
        //编号
        String fdNumber = jsonObject.getString("fd_number");
        //拟稿时间
        String docCreateTime = jsonObject.getString("doc_create_time");
        //流程状态
        String docStatus = jsonObject.getString("doc_status");
        //流程标题
        String docSubject = jsonObject.getString("doc_subject");
        responseVO.setReviewOaName(docSubject);
        responseVO.setReviewOaUrl(mhtUrl);
        responseVO.setReviewOaId(fdNumber);
        return responseVO;
    }
}
